import 'dart:convert';

import 'package:flutter/material.dart';

class TestDidChangeDependencies2 extends StatefulWidget {
  const TestDidChangeDependencies2({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => TestDidChangeDependencies2State();
}

class TestDidChangeDependencies2State extends State<TestDidChangeDependencies2> {
  bool bDependenciesShouldChange = true;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      print('build 第一帧回调后 doing Build 值为:${context.debugDoingBuild}');
    });
    String afterStr = toHalfWidth('诉讼费）。');
    print('--------------------$afterStr');
  }

  Future<String> getFutureString() async {
    return '你好';
  }
  String toHalfWidth(String input) {
    final bytes = utf8.encode(input);
    final result = bytes.map((byte) {
      if (byte >= 0xE38080 && byte <= 0xE3808D) { // 全角中文符号
        return byte - 0xDBC0;
      } else if (byte == 0xE3809C) { // 全角双引号
        return 0x22;
      } else if (byte == 0xE3809D) { // 全角单引号
        return 0x27;
      } else if (byte == 0xE38184) { // 全角冒号
        return 0x3A;
      } else if (byte == 0xE38186) { // 全角分号
        return 0x3B;
      } else if (byte == 0xE38188) { // 全角问号
        return 0x3F;
      } else if (byte == 0xE3818D) { // 全角句号
        return 0x2E;
      }
      return byte;
    }).toList();
    return utf8.decode(result);
  }
  @override
  Widget build(BuildContext context) {
    print('build 被执行时 doing Build 值为${context.debugDoingBuild}');
    return Scaffold(
        body: bDependenciesShouldChange
            ? Container(
                height: 500,
                alignment: Alignment.centerLeft,
                child: C(child: B()),
              )
            : Container(
                height: 500,
                alignment: Alignment.centerLeft,
                child: C(child: SizedBox(width: 20, height: 50, child: B())),
              ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ));
  }

  void _incrementCounter() {
    setState(() {
      bDependenciesShouldChange = !bDependenciesShouldChange;
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    debugPrint("A-didChangeDependencies");
  }
}

class B extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => BState();
}

class BState extends State<B> {
  @override
  Widget build(BuildContext context) {
    return const Center(child: Text("Test"));
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    debugPrint("B-didChangeDependencies");
  }
}

class C extends StatefulWidget {
  final Widget child;

  C({Key? key, required this.child}) : super(key: key);

  @override
  State<StatefulWidget> createState() => CState();
}

class CState extends State<C> {
  @override
  Widget build(BuildContext context) {
    return widget.child;
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    debugPrint("C-didChangeDependencies");
  }
}

class D extends StatelessWidget {
  final Widget childD;

  D({required this.childD});

  @override
  Widget build(BuildContext context) {
    return childD;
  }
}
